from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from plone.app.layout.viewlets.common import ViewletBase
from Products.Five.browser import BrowserView
from Products.CMFCore.utils import getToolByName
from Products.CMFPlone.utils import base_hasattr

class uriachSearchView(BrowserView):
    
    render = ViewPageTemplateFile("templates/uriach_search_view.pt")
    
    def listChildFolders(self):
        catalog = getToolByName(self.context, 'portal_catalog')
        
        path = '/'.join(self.context.getPhysicalPath())
        
        return catalog(portal_type='Folder',
                       path=path, 
                       review_state=['published', 'external', 'web_published','internally_published'], 
                       sort_on='getObjPositionInParent',)
    
    def getPath(self):
        return '/'.join(self.context.getPhysicalPath())
    
    def test(self, value, trueVal, falseVal):
        """
            helper method, mainly for setting html attributes.
        """
        if value:
            return trueVal
        else:
            return falseVal


class uriachFileSearchView(BrowserView):
    
    render = ViewPageTemplateFile("templates/uriach_filesearch_view.pt")
    
    def listChildFolders(self):
        catalog = getToolByName(self.context, 'portal_catalog')
        
        path = '/'.join(self.context.getPhysicalPath())
        
        return catalog(portal_type='UriachFile',
                       path=path, 
                       review_state=['published', 'external', 'web_published','internally_published'], 
                       sort_on='getObjPositionInParent',)
    
    def getPath(self):
        return '/'.join(self.context.getPhysicalPath())
    
    def test(self, value, trueVal, falseVal):
        """
            helper method, mainly for setting html attributes.
        """
        if value:
            return trueVal
        else:
            return falseVal

class uriachFileView(BrowserView):
    
    render = ViewPageTemplateFile("templates/uriach_file_view.pt")
    
    def test(self, bool, yes, no):
        #import pdb;pdb.set_trace()
        if bool:
            return yes
        else:
            return no
    
class uriachDirectFileView(BrowserView):
    
    render = ViewPageTemplateFile("templates/uriach_direct_file_view.pt")
    
    def setUriachFileContentFilter(self):
        myFilter = {}
        myFilter['portal_type'] = 'UriachFile'
        myFilter['review_state']=['published', 'external', 'web_published','internally_published']
        myFilter['sort_on']='getObjPositionInParent'
        return myFilter
    
    def test(self, bool, yes, no):
        #import pdb;pdb.set_trace()
        if bool:
            return yes
        else:
            return no

class uriachAlbumView(BrowserView):
    
    render = ViewPageTemplateFile("templates/uriach_album_view.pt")
    image_amount = None
    
    def uriachListAlbum(self, context=None, images=0, folders=0, subimages=0, others=0):
            
        if context is None:
            context = self.context
            
        result = {}
        
        if context.portal_type == 'Topic':
            queryMethod = context.queryCatalog
        else:
            queryMethod = context.getFolderContents
        
        if images:
            result['images'] = queryMethod({'portal_type':('Image',)},full_objects=True)
        if folders:
            # We don't need the full objects for the folders
            result['folders'] = queryMethod({'portal_type':('Folder',)})
        if subimages:
            #Handle brains or objects
            if base_hasattr(context, 'getPath'):
                path = context.getPath()
            else:
                path = '/'.join(context.getPhysicalPath())
            # Explicitly set path to remove default depth
            self.image_amount = len(queryMethod({'Type':('Image',), 'path':path}))
            #uriach special change, to catch only the images at first level folder
            result['subimages'] = queryMethod({'Type':('Image',), 'path':dict(query=path, depth=1)})
            
        if others:
            searchContentTypes = context.plone_utils.getUserFriendlyTypes()
            filtered = [p_type for p_type in searchContentTypes
                        if p_type not in ('Image', 'Folder',) ]
            if filtered:
                # We don't need the full objects for the folder_listing
                result['others'] = queryMethod({'portal_type':filtered})
            else:
                result['others'] = ()
        
        return result
    
    def test(self, bool, yes, no):
        #import pdb;pdb.set_trace()
        if bool:
            return yes
        else:
            return no